{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "import queue\n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "\n",
    "class BatchDataThread(threading.Thread):\n",
    "    def __init__(self, queue):\n",
    "        super(BatchDataThread, self).__init__()\n",
    "        self.queue = queue\n",
    "        self.start()\n",
    "    \n",
    "    def run(self):\n",
    "        while not self._is_stopped:\n",
    "            if self.queue.qsize() < 20:\n",
    "                batch_X = np.random.choice(np.arange(100), 10)\n",
    "                batch_Y = (1, 2, 3)\n",
    "                put_tuple = (batch_X, batch_Y)\n",
    "                self.queue.put(put_tuple)\n",
    "            time.sleep(0.0001)            \n",
    "            \n",
    "class BatchDataGenerator(object):\n",
    "    def __init__(self, worker_quantity=4):\n",
    "        self.queue = queue.Queue()\n",
    "        for i in range(worker_quantity):\n",
    "            BatchDataThread(self.queue)\n",
    "              \n",
    "    def get_next(self):\n",
    "        get_tuple = self.queue.get()\n",
    "        return get_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "batchData_generator = BatchDataGenerator()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(array([95, 33, 43, 18, 57, 21, 42, 42, 42, 72]), (1, 2, 3))\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "print(batchData_generator.get_next())\n",
    "print(batchData_generator.queue.qsize())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
